//
//  AccountPsdView.m
//  商城
//
//  Created by runone02 on 2018/8/29.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "AccountPsdView.h"
#import "UIView+Toast.h"
#import "DCHandPickViewController.h"
#import "DCBeautyMessageViewController.h"
#import "DCMediaListViewController.h"
#import "DCBeautyShopViewController.h"

@interface AccountPsdView()<UITextFieldDelegate>
/** */
@property (nonatomic,strong) UIImageView *accountImage;
/** */
@property (nonatomic,strong) UIImageView *pasdImage;
/** 账号*/
@property (nonatomic,strong) UITextField *accoutTF;
/** 账号*/
@property (nonatomic,strong) UITextField *passdTF;
/** 快速注册*/
@property (nonatomic,strong) UIButton *fastRegisterButton;
/** 忘记密码*/
@property (nonatomic,strong) UIButton *forgetPasswordButton;
/** 登录按钮*/
@property (nonatomic,strong) UIButton *loginButton;
/** 协议*/
@property (nonatomic,strong) UIButton *protocolButton;
@end

@implementation AccountPsdView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.userInteractionEnabled = YES;
    
    self.accountImage = [[UIImageView alloc]init];
    self.accountImage.image = IMAGE(@"myb_set_kaihu");
    [self addSubview:self.accountImage];
    [self.accountImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).with.offset(10);
        make.left.mas_equalTo(self).with.offset(10);
        make.width.height.mas_equalTo(20);
    }];
    
    self.accoutTF = [[UITextField alloc]init];
    self.accoutTF.placeholder = @"请输入账号";
    self.accoutTF.userInteractionEnabled = YES;
    self.accoutTF.font = TITLE_FONT(30);
    [self addSubview:self.accoutTF];
    [self.accoutTF mas_makeConstraints:^(MASConstraintMaker *make){
        make.centerY.mas_equalTo(self.accountImage);
        make.left.mas_equalTo(self.accountImage.mas_right).with.offset(10);
        make.right.mas_equalTo(self).with.offset(10);
        make.height.mas_equalTo(30);
    }];
    
    UIView *lineOneView = [[UIView alloc]init];
    lineOneView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:lineOneView];
    [lineOneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.accoutTF.mas_bottom).with.offset(5);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
        make.height.mas_equalTo(1);
    }];
    
    self.pasdImage = [[UIImageView alloc]init];
    self.pasdImage.image = IMAGE(@"myb_set_password");
    [self addSubview:self.pasdImage];
    [self.pasdImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lineOneView.mas_bottom).with.offset(30);
        make.left.mas_equalTo(self).with.offset(10);
        make.width.height.mas_equalTo(20);
    }];
    
    self.passdTF = [[UITextField alloc]init];
    self.passdTF.userInteractionEnabled = YES;
    self.passdTF.font = TITLE_FONT(30);
    self.passdTF.placeholder = @"请输入密码";
    [self addSubview:self.passdTF];
    [self.passdTF mas_makeConstraints:^(MASConstraintMaker *make){
        make.centerY.mas_equalTo(self.pasdImage);
        make.left.mas_equalTo(self.pasdImage.mas_right).with.offset(10);
        make.right.mas_equalTo(self).with.offset(10);
        make.height.mas_equalTo(30);
    }];
    
    UIView *lineTowView = [[UIView alloc]init];
    lineTowView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:lineTowView];
    [lineTowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.passdTF.mas_bottom).with.offset(5);
        make.left.mas_equalTo(self).with.offset(10);
        make.right.mas_equalTo(self).with.offset(-10);
        make.height.mas_equalTo(1);
    }];
    
    self.fastRegisterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.fastRegisterButton setTitle:@"手机快速注册" forState:UIControlStateNormal];
    [self.fastRegisterButton addTarget:self action:@selector(fastRegisterButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.fastRegisterButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.fastRegisterButton.titleLabel.font = TITLE_FONT(26);
    [self addSubview:self.fastRegisterButton];
    [self.fastRegisterButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).with.offset(10);
        make.top.mas_equalTo(lineTowView.mas_bottom).with.offset(20);
        make.width.mas_equalTo(SCREEN_WIDTH/4);
        make.height.mas_equalTo(20);
    }];
    
    self.forgetPasswordButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.forgetPasswordButton setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [self.forgetPasswordButton addTarget:self action:@selector(forgetPasswordButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.forgetPasswordButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.forgetPasswordButton.titleLabel.font = TITLE_FONT(26);
    [self addSubview:self.forgetPasswordButton];
    [self.forgetPasswordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self).with.offset(-10);
        make.top.mas_equalTo(lineTowView.mas_bottom).with.offset(20);
        make.width.mas_equalTo(SCREEN_WIDTH/4);
        make.height.mas_equalTo(20);
    }];
    
    self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [self.loginButton addTarget:self action:@selector(loginButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.loginButton.backgroundColor = [UIColor lightGrayColor];
    self.loginButton.titleLabel.font = TITLE_FONT(36);
    
    [self.loginButton.layer setMasksToBounds:YES];
    [self.loginButton.layer setCornerRadius:1.0];
    
    [self.loginButton.layer setBorderWidth:1.0];
    self.loginButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    

    [self addSubview:self.loginButton];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.top.mas_equalTo(self.forgetPasswordButton.mas_bottom).with.offset(30);

        make.height.mas_equalTo(50);
    }];
    
    self.protocolButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.protocolButton setTitle:@"登录成功即代表同意《服务协议》" forState:UIControlStateNormal];
    self.protocolButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
      [self.protocolButton addTarget:self action:@selector(protocolButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.protocolButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    self.protocolButton.titleLabel.font = TITLE_FONT(26);
    [self addSubview:self.protocolButton];
    [self.protocolButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).with.offset(10);
        make.top.mas_equalTo(self.loginButton.mas_bottom).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-10);
        make.height.mas_equalTo(20);
    }];
}


- (void)protocolButtonClick:(UIButton *)btn{
    NSLog(@"遵守协议");
}

- (void)loginButtonClick:(UIButton *)btn{
    NSLog(@"登录");
    [self endEditing:YES];
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    WEAKSELF
    if ([self.accoutTF.text isEqualToString:@"CDDMall"] && [self.passdTF.text isEqualToString:@"000"]) {
        
        [DCObjManager dc_saveUserData:@"1" forKey:@"isLogin"]; //1代表登录
        [DCObjManager dc_saveUserData:self.accoutTF.text forKey:@"UserName"]; //记录用户名
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf makeToast:@"登录成功" duration:0.5 position:CSToastPositionCenter];
            [weakSelf setUpUserBaseData];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication].keyWindow.rootViewController dismissViewControllerAnimated:YES completion:^{
                    [weakSelf endEditing:YES];
                    NSLog(@"VC:%@  %@",[[DCSpeedy dc_getCurrentVC] class],[DCHandPickViewController class]);
                    if ([@[[DCHandPickViewController class],[DCBeautyMessageViewController class],[DCMediaListViewController class],[DCBeautyShopViewController class]] containsObject:[[DCSpeedy dc_getCurrentVC] class]]) { //过滤
                        [[NSNotificationCenter defaultCenter]postNotificationName:LOGINSELECTCENTERINDEX object:nil];
                    }
                }];
            });
        });
        
    }else{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf makeToast:@"账号密码错误请重新登录" duration:0.5 position:CSToastPositionCenter];
        });
    }
}

- (void)forgetPasswordButtonClick:(UIButton *)btn{
    NSLog(@"忘记密码");
}


- (void)fastRegisterButtonClick:(UIButton *)btn{
    NSLog(@"快速注册");
}

#pragma mark - 设置初始数据
- (void)setUpUserBaseData{
    DCUserInfo *userInfo = UserInfoData;
    if (userInfo.username.length == 0) { //userName为指定id不可改动用来判断是否有用户数据
        DCUserInfo *userInfo = [[DCUserInfo alloc] init];
        userInfo.nickname = @"RocketsChen";
        userInfo.sex = @"男";
        userInfo.birthDay = @"1996-02-10";
        userInfo.userimage = @"icon";
        userInfo.username = @"qq-w923740293";
        userInfo.defaultAddress = @"中国 上海";
        dispatch_async(dispatch_get_global_queue(0, 0), ^{//异步保存
            [userInfo saveOrUpdate];
        });
    }
}

@end
